﻿using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Chbl.Common.Factories;
using Chbl.DataAccess.DTO;
using Chbl.DataAccess.Properties;
using Dapper;

namespace Chbl.DataAccess.Repositories
{
    public class GalleryServerProRepository: IGalleryServerProRepository
    {
        private readonly string _connectionString;
        public GalleryServerProRepository(IChblConnectionFactory chblConnectionFactory)
        {
            _connectionString = chblConnectionFactory.GetConnectionString();
        }

        public MediaObject GetMediaObject(int id)
        {
            using (DbConnection connection = CreateConnection())
            {
                connection.Open();
                return connection.Query<MediaObject>(SqlResources.GSP_GetMediaObject, new { ObjectId = id }).First();    
            }            
        }

        public IEnumerable<MediaObject> GetAlbumMediaObjects(int albumId)
        {
            using (DbConnection connection = CreateConnection())
            {
                connection.Open();
                return connection.Query<MediaObject>(SqlResources.GSP_GetAlbumMediaObjects, new { AlbumId = albumId });
            }    
        }

        private DbConnection CreateConnection()
        {
            return new SqlConnection(_connectionString);
        }
    }
}
